<?php

namespace App\Console\Commands;

use App\Models\DictPayment;
use App\Models\PlatUser;
use App\Models\RechargeOrder;
use App\Services\PlatUserService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class AutoUpperSettle extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'handle:upper_settle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @param \App\Services\PlatUserService $platUserService
     *
     * @return mixed
     */
    public function handle(PlatUserService $platUserService)
    {
        //
        $orders = RechargeOrder::where('order_status', 1)
            ->where('proxy', '<>', 0)
            ->where('proxy_settle', 0)
            ->get();
        foreach ($orders as $order) {
            $platUser = $order->platuser;
            $payment = DictPayment::where('identify', $order->order_data['recharge_type'])->first();
            $payment = $platUserService->getRechargePayment($platUser, $payment);
            $upper = $platUserService->getUppersWithSettle($order->order_amt, $platUser, $payment);
            $order->update($upper);
        }
    }
}
